// store/modules/preorder.js
import axios from 'axios';

export default {
    namespaced: true,
    state: {
        preOrderNo: null,
        error: null,
    },
    mutations: {
        SET_PRE_ORDER_NO(state, preOrderNo) {
            state.preOrderNo = preOrderNo;
        },
        SET_ERROR(state, error) {
            state.error = error;
        },
    },
    actions: {
        async createPreOrder({ commit }, preOrderData) {
            try {
                const response = await axios.post('order/api/order/pre/order/travel', preOrderData);

                if (response.data.code === 200) {
                    commit('SET_PRE_ORDER_NO', response.data.data.preOrderNo);
                    return { success: true, preOrderNo: response.data.data.preOrderNo };
                } else {
                    commit('SET_ERROR', response.data.message || '预下单失败');
                    return { success: false, error: response.data.message || '预下单失败' };
                }
            } catch (error) {
                console.error('预下单失败:', error);
                const errorMessage = '预下单失败，请稍后再试';
                commit('SET_ERROR', errorMessage);
                return { success: false, error: errorMessage };
            }
        },
    },
    getters: {
        getPreOrderNo: state => state.preOrderNo,
        getError: state => state.error,
    },
};
